#include "adddialog.h"


AddDialog::AddDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::addDialog)
{
    ui->setupUi(this);
    initData();
}

AddDialog::~AddDialog()
{
    delete ui;
}

void AddDialog::initData()
{
    stu2ui();
}

void AddDialog::setModel(QStandardItemModel *model)
{
    this->model = model;
}

void AddDialog::on_buttonBox_clicked(QAbstractButton *button)
{
    QString localText = button->text();
    if(localText == "OK"){
        ui2stu();
        appendItem();
    }else{
        this->close();
    }
}

void AddDialog::stu2ui()
{
    ui->lineEdit->setText(stu.getNum());
    ui->lineEdit_2->setText(stu.getName());
    ui->lineEdit_3->setText(stu.getAddr());
    ui->spinBox->setValue(stu.getAge());
}

void AddDialog::ui2stu()
{
    stu.setNum(ui->lineEdit->text());
    stu.setName(ui->lineEdit_2->text());
    stu.setAge(ui->spinBox->value());
    stu.setAddr(ui->lineEdit_3->text());
}

void AddDialog::appendItem()
{
    QList<QStandardItem*> items;
    items << new QStandardItem(stu.getNum());
    items << new QStandardItem(stu.getName());
    items << new QStandardItem(QString().setNum(stu.getAge()));
    items << new QStandardItem(stu.getAddr());
    this->model->appendRow(items);
}

Student AddDialog::getStu() const
{
    return stu;
}

void AddDialog::setStu(const Student &value)
{
    stu = value;
}
